package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message is used to activate signaling of incoming events from
 * COMMON-ISDN-API to the application.
 *
 * <p>Info mask is used to define which signaling protocol events are to
 * be indicated to the application. These events are normally associated with
 * physical connections. CIP Mask defines selection criteria based upon
 * Bearer Capability and High Layer Compatibility, thus specifying which
 * incoming calls are signaled to an application.
 *
 * <p>More than one application may listen to the same CIP Values. Every
 * application listening to a matching value is informed about incoming
 * calls. If more than one application attempts to accept the call, the
 * first CONNECT_RESP received by COMMON-ISDN-API is accepted. Every other
 * application receives a DISCONNECT_IND message which indicates this
 * situation in the Reason parameter.
 */
public class ListenReq
        extends CapiActiveMessage
        implements ControllerProperty {

    /** The controller number. */
    private int _controller = 1;

    /** The Info mask. */
    private InfoMask _infoMask = new InfoMask();

    /** The CIP mask. */
    private CipMask _cipMask = new CipMask();

    /** The calling party number. */
    private CallingPartyNumber _callingPartyNumber = new CallingPartyNumber();

    /** The calling party subaddress. */
    private SubAddress _callingPartySubAddress = new SubAddress();

    /**
     * Create a new {@code ListenReq} message.
     */
    public ListenReq() {
        super(Command.LISTEN_REQ);
    }

    /** {@inheritDoc} */
    public int getController() {
        return _controller;
    }

    /** {@inheritDoc} */
    public void setController(final int controller) {
        _controller = controller;
    }

    /**
     * Retrieve the Info mask, encapsulated in an {@code InfoMask} object.
     *
     * @return The {@code InfoMask}.
     */
    public InfoMask getInfoMask() {
        return _infoMask;
    }

    /**
     *
     * @param infoMask
     */
    public void setInfoMask(final InfoMask infoMask) {
        _infoMask = infoMask;
    }

    /**
     * Retrieve the CIP mask.
     *
     * @return The CIP mask.
     */
    public CipMask getCipMask() {
        return _cipMask;
    }

    /**
     *
     * @param cipMask
     */
    public void setCipMask(final CipMask cipMask) {
        _cipMask = cipMask;
    }

    /** {@inheritDoc} */
    protected void serializeTo(final ByteBuffer byteBuffer) {
        byteBuffer.putInt(_controller);
        _infoMask.writeTo(byteBuffer);
        _cipMask.writeTo(byteBuffer);
        byteBuffer.putInt(0); // CIP Mask 2 (reserved for additional services)
        _callingPartyNumber.writeTo(byteBuffer);
        _callingPartySubAddress.writeTo(byteBuffer);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(getClass().getSimpleName());
        sb.append("[").append(super.toString());
        sb.append(" ctrl=").append(getController());
        sb.append(" infoMask=").append(getInfoMask());
        sb.append(" cipMask=").append(getCipMask());
        sb.append(" calling#=").append(_callingPartyNumber);
        sb.append(" callingSub#=").append(_callingPartySubAddress);
        sb.append("]");

        return sb.toString();
    }

}
